<?php

namespace Console\Controller;

use Console\Common\Library\Page;

class MyJoinController extends CstBaseController
{
    private $taskLogic;
    private $joinLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->taskLogic = A('Console/Task', 'Logic');
        $this->joinLogic = A('Console/Join', 'Logic');
    }

    public function joinList()
    {
        $typeDic = $this->taskLogic->getTypeDic();
        $typeDic[10] = '问卷赚';

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $taskIdsRes = $this->joinLogic->getTaskIdList(array(
            'admin_user_id' => $userId,
            'state' => array('in', '2,3,-2'),
        ));
        $taskIdsArr = $taskIdsRes['Result'];
        $taskIds = array();
        foreach ($taskIdsArr as $vo) {
            $taskIds[] = $vo['task_id'];
        }
        if (empty($taskIds)) {
            $this->sysErr('无参与任务');
        }

        $taskListRes = $this->taskLogic->getList(0, 1000, array(
            'id' => array('in', $taskIds),
        ));
        $taskList = $taskListRes['Result']['list'];

        $this->assign('typeDic', $typeDic);
        $this->assign('taskList', $taskList);
        $this->display();
    }

    /**
     * 获取列表.
     */
    public function doGetMyJoinList()
    {
        $data = I('post.');
        $state = $data['state'];
        $page = $data['page'] ? $data['page'] : 1;
        $pageSize = 20;
        unset($data['page']);

        if (empty($state)) {
            $data['state'] = array('in', '2,3,-2');
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $data['admin_user_id'] = array('in', $userId.',1000000');

        $listRes = $this->joinLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        foreach ($list as &$vo) {
            $proof = $vo['proof'];
            if (!empty($proof)) {
                $vo['proof'] = explode(',', $proof);
            }
        }

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->taskLogic->getTypeDic();
        $typeDic[10] = '问卷赚';

        $this->assign('typeDic', $typeDic);
        $this->assign('total', $total);
        $this->assign('pageTotal', $pageTotal);
        $this->assign('pageHtml', $pageHtml);
        $this->assign('list', $list);
        $this->assign('pageHtml', $pageHtml);
        $html = $this->fetch('doGetMyJoinList');

        $res = array(
            'list' => $list,
            'html' => $html,
            'total' => $total,
            'pageTotal' => $pageTotal,
        );
        $this->ajaxReturn(static::$res->suc('成功', $res));
    }

    /**
     * 审核加入的任务
     *
     * @return void
     */
    public function doAuditJoin()
    {
        $id = I('post.id');
        $state = I('post.state');
        $state = 1 == $state ? 3 : -2;

        if (empty($id)) {
            $this->ajaxReturn(static::$res->err('参与凭证不能为空'));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->joinLogic->getInfo(array(
            'id' => $id,
            'admin_user_id' => $userId,
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err('参与记录未找到'));
        }

        $res = $this->joinLogic->auditJoin($userId, $id, $state);
        $this->ajaxReturn($res);
    }
}
